package com.yanqu.road.dao.impl.activity.rankunionwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.rankunionwar.data.RankUnionWarUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RankUnionWarUserDataDaoImpl extends TempDataObjectDao<RankUnionWarUserData> {


    public static String getUserDataSql = "select * from t_u_activity_rank_union_war where activityId=? and userId=?";
    public RankUnionWarUserData getUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDataSql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setLong(i++,userId);
            rs = call.executeQuery();
            if (rs.next()) {
                RankUnionWarUserData data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserDataSql = "insert into t_u_activity_rank_union_war(" +
            "activityId,userId,passTimes,floor,bigRewardPosition,floorOpenData,lookRewardData) " +
            " values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(RankUnionWarUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBigRewardPosition()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getFloorOpenMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getLookRewardMapList())));

        if (getDb().executeNoneQuery(insertUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDataSql = "update t_u_activity_rank_union_war set " +
            " passTimes=?,floor=?,bigRewardPosition=?,floorOpenData=?,lookRewardData=?" +
            " where activityId=? and userId=?";
    @Override
    protected boolean updateData(RankUnionWarUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getPassTimes()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getFloor()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBigRewardPosition()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getFloorOpenMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONArray.toJSONString(data.getLookRewardMapList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected RankUnionWarUserData parseData(ResultSet rs) throws SQLException {
        RankUnionWarUserData data = new RankUnionWarUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPassTimes(rs.getInt("passTimes"));
        data.setFloor(rs.getInt("floor"));
        data.setBigRewardPosition(rs.getInt("bigRewardPosition"));
        String floorOpenStr  = rs.getString("floorOpenData");
        if(!StringUtils.isNullOrEmpty(floorOpenStr)){
            Map<Integer, String> result = JSON.parseObject(floorOpenStr, new TypeReference<Map<Integer, String>>() {});
            if(result != null){
                data.setFloorOpenMap(result);
            }
        }
        String lookRewardDataStr  = rs.getString("lookRewardData");
        if(!StringUtils.isNullOrEmpty(lookRewardDataStr)){
            List<Integer> result = JSON.parseObject(lookRewardDataStr, new TypeReference<List<Integer>>() {});
            if(result != null){
                data.setLookRewardMapList(result);
            }
        }else{
            data.setLookRewardMapList(new ArrayList<>());
        }
        return data;
    }

}
